package rattlesnake.callback.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package: rattlesnake.callback.util
 * @Description: 主要运用反射技术，获得类字段信息，加载配置文件使用
 * @author: liuxin
 * @date: 17/4/10 下午5:20
 */
public class ClassUtil {
    private static final Logger logger= LoggerFactory.getLogger(ClassUtil.class);

    public static List<String> getCategoryURl(String classPath) {
        Class cls = null;
        List<String> categoryList = new ArrayList<String>();
        try {
            cls = Class.forName(classPath);
            Field[] fields = cls.getDeclaredFields();
            for (Field field : fields) {
                logger.debug(field.getName() + "\t:" + String.valueOf(field.get(field.getName())));
                categoryList.add(String.valueOf(field.get(field.getName())));
            }
        } catch (ClassNotFoundException cnf) {
            throw new RuntimeException("渠道配置加载出错", cnf);
        } catch (IllegalAccessException iae) {
            throw new RuntimeException("渠道配置出错", iae);
        }
        return categoryList;
    }


    public static List<String> getChannelName(String classPath) {
        Class cls = null;
        List<String> channelNames = new ArrayList<String>();
        try {
            cls = Class.forName(classPath);
            Field[] fields = cls.getDeclaredFields();
            for (Field field : fields) {
                logger.debug(field.getName() + "\t:" + String.valueOf(field.get(field.getName())));
                channelNames.add(String.valueOf(field.get(field.getName())));
            }
        } catch (ClassNotFoundException cnf) {
            throw new RuntimeException("渠道配置加载出错", cnf);
        } catch (IllegalAccessException iae) {
            throw new RuntimeException("渠道配置出错", iae);
        }
        return channelNames;
    }

}
